package com.qfedu.servlet;

import com.qfedu.dao.IAddressDao;
import com.qfedu.dao.IItemDao;
import com.qfedu.dao.IProductDao;
import com.qfedu.dao.impl.AddressDaoImpl;
import com.qfedu.dao.impl.ItemDaoImpl;
import com.qfedu.dao.impl.ProductDaoImpl;
import com.qfedu.entity.*;
import com.qfedu.service.ICartService;
import com.qfedu.service.IOrderService;
import com.qfedu.service.impl.CartServiceImpl;
import com.qfedu.service.impl.OrderServiceImpl;
import com.qfedu.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/order")
public class OrderServlet extends BaseServlet {
    private IAddressDao addressDao = new AddressDaoImpl();
    private IOrderService orderService = new OrderServiceImpl();
    private ICartService cartService = new CartServiceImpl();
    private IItemDao itemDao = new ItemDaoImpl();
    private IProductDao productDao = new ProductDaoImpl();
    public void queryOrderList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
//        String aidStr = request.getParameter("aid");
//        int aid = Integer.parseInt(aidStr);
        String detailId = (String) request.getSession().getAttribute("detailId");
        Result result = orderService.queryOrderList(uid);
        List<Order> orderList = (List<Order>) result.getDate();
        request.getSession().setAttribute("orderList",orderList);
        try {
            request.getRequestDispatcher("orderList.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void goOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        int uid = user.getId();
        long l = System.currentTimeMillis();
        String id = l+"";
        String aidStr = request.getParameter("aid");
        int aid = Integer.parseInt(aidStr);
        request.getSession().setAttribute("aid",aid);
        String moneyStr = request.getParameter("money");
        orderService.insertOrder(uid,Integer.parseInt(aidStr),Double.parseDouble(moneyStr));
        request.getSession().setAttribute("money",moneyStr);
        request.getSession().setAttribute("detailId",id);
        List<Cart> cartList = (List<Cart>) request.getSession().getAttribute("cartList");
        try {
            JDBCUtil.begin();
            for (Cart cart : cartList) {
                //JDBCUtil.begin();
                int pid = cart.getPid();
                int count = cart.getCount();
                double imoney = cart.getCount()*Double.parseDouble(cart.getPprice());
                itemDao.insertItem(id,pid,imoney,count);
            }
            cartService.clearCart(uid);
            JDBCUtil.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            JDBCUtil.rollback();
        }
        try {
            request.getRequestDispatcher("orderSuccess.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public void goOrderDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String oid = request.getParameter("oid");
        List<Product> products = productDao.queryProductList(oid);
        request.getSession().setAttribute("products1",products);
        Order order = orderService.findOrderList(oid);
        request.getSession().setAttribute("order1",order);
        try {
            request.getRequestDispatcher("orderDetail.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
}